﻿
$(function () {
    $('.postbacker').live('click', function () { $('#__EVENTTARGET').val(this.name); })
    $('input').live('keyup', keyboardEventData.removeKey).live('keydown', keyboardEventData.addKey);
    $('form').live('submit', function () {
        $.ajax({
            type: "POST",
            url: this.action,
            data: $(this).serialize(),
            dataType: 'script'
        });
        return false;
    });
});
keyboardEventData = { key: UIT.Events.Keyboard.Keys.None, lastKeyPressed: 0,
    addKey: function (ev) {
        var keyCode = ev.which;
        keyboardEventData.lastKeyPressed = keyCode;
        var key = keyboardEventData.key;
        if (keyboardEventData.lastKeyPressed == UIT.Events.Keyboard.Keys.ShiftKey || keyboardEventData.lastKeyPressed == UIT.Events.Keyboard.Keys.LShiftKey || keyboardEventData.lastKeyPressed == UIT.Events.Keyboard.Keys.RShiftKey)
            key = key | UIT.Events.Keyboard.Keys.Shift;
        else if (keyboardEventData.lastKeyPressed == UIT.Events.Keyboard.Keys.ControlKey || keyboardEventData.lastKeyPressed == UIT.Events.Keyboard.Keys.LControlKey || keyboardEventData.lastKeyPressed == UIT.Events.Keyboard.Keys.RControlKey)
            key = key | UIT.Events.Keyboard.Keys.Shift;
        else if (keyboardEventData.lastKeyPressed == UIT.Events.Keyboard.Keys.Menu || keyboardEventData.lastKeyPressed == UIT.Events.Keyboard.Keys.LMenu || keyboardEventData.lastKeyPressed == UIT.Events.Keyboard.Keys.RMenu)
            key = key | UIT.Events.Keyboard.Keys.Alt;
        else
            key = key | keyCode;

        keyboardEventData.key = key;
    },
    removeKey: function (ev) {
        keyboardEventData.key -= ev.which;
    }

};

function keyDown(commandName, domElement, keyCode) {
    $(domElement).keydown(keyboardEventData.addKey).keyup(keyboardEventData.removeKey);
    $(domElement).keydown(function (ev) {
        if (keyboardEventData.key == keyCode)
            Command.runOn(this, commandName);
    });
}

(function ($) {
    $.extend($.fn, { datagrid: function (arg) {
        if (typeof (arg) == 'string') {
            if (arg === 'next') {
                var settings = this.data('grid');
                settings = $.extend({ page: 0 }, settings);
                if (Number(settings.page) < Number(this.find('td.pager a:not(.next):last').text()) - 1)
                    this.datagrid.call(this, { page: settings.page + 1 });
            }
            else if (arg === 'prev') {
                var settings = this.data('grid');
                settings = $.extend({ page: 0 }, settings);
                if (settings.page > 0)
                    this.datagrid.call(this, { page: settings.page - 1 });
            }
            else if (arg.substr(0, 5) == 'page-') {
                this.datagrid.call(this, { page: arg.substr(5) - 1 });
            }
            else if (arg.substr(0, 5) == 'sort-') {
                this.datagrid.call(this, { sort: arg.substr(5) });
            }
            else
                alert('unhandled arg');
        }
        else {
            this.data('grid', $.extend({}, this.data('grid') || {}, arg));
            $('#__EVENTTARGET').val(this.attr('id'));
            var settings = "";
            $.each(this.data('grid'), function (key, value) {
                settings += key + "=" + value + "&";
            });
            $('input[type=hidden][name=' + this.attr('id') + ']').val(settings);
            $(this).parents('form').submit();
        }
    }
    });
})(jQuery);

$(function () {
    $('.datagrid th[data-sort], .datagrid .pager .ui-state-default').on('mouseenter mouseleave', function () {
        $(this).toggleClass('ui-state-hover');
    });

    $('.datagrid th[data-sort]')
    .on('click', function () {
        var datagrid = $(this).parents('.datagrid');
        var sortBy = $(this).attr('data-sort');
        datagrid.datagrid('sort-' + sortBy);

        $(this).find('.ui-icon').remove();

        if (sortBy.substr(-4) == 'DESC') {
            $(this).attr('data-sort', sortBy.substr(0, sortBy.length - 5)).append($('<span class="ui-icon ui-icon-triangle-1-s"></span>"'));
        }
        else {
            $(this).attr('data-sort', sortBy + ' DESC').append($('<span class="ui-icon ui-icon-triangle-1-n"></span>"'));
        }
        var oldSort = datagrid.find('th.ui-state-active');
        if (oldSort.length > 0) {
            var oldSortValue = oldSort.attr('data-sort');
            if (sortBy + ' DESC' != oldSortValue && oldSortValue.substr(-4) == 'DESC')
                oldSort.attr('data-sort', oldSortValue.substr(0, oldSortValue.length - 5));
            if (sortBy + ' DESC' != oldSortValue && sortBy != oldSortValue + ' DESC') {
                oldSort.removeClass('ui-state-active').find('.ui-icon').remove()
                oldSort.append($('<span class="ui-icon ui-icon-triangle-2-n-s"></span>'));
            }
        }
        $(this).addClass('ui-state-active');
    }).append($('<span class="ui-icon ui-icon-triangle-2-n-s"></span>'));

    $('.datagrid .pager .ui-state-default:has(a:not(.prev):not(.next))')
    .on('click', function () {
        var datagrid = $(this).parents('.datagrid');
        $(this).parents('.pager').find('.ui-state-active').removeClass('ui-state-active');
        $(this).addClass('ui-state-active');
    });

    $('.datagrid .pager .ui-state-default .prev')
    .on('click', function () {
        var datagrid = $(this).parents('.datagrid');
        var pager = $(this).parents('.pager').find('td');
        pager.eq(pager.filter('.ui-state-active').index() - 1).find('a').click();
    });
    $('.datagrid .pager .ui-state-default .next')
    .on('click', function () {
        var datagrid = $(this).parents('.datagrid');
        var pager = $(this).parents('.pager').find('td');
        pager.eq(pager.filter('.ui-state-active').index() + 1).find('a').click();
    });
});